import { createRouter, createWebHashHistory } from 'vue-router'

import main from '@/views/Main.vue'
import home from '@/views/Main/Home.vue'
import AuthorMessage from '@/views/about/AuthorMessage.vue'
import AuthorFormate from '@/components/AuthorFormate.vue'

import { getPersonMsg } from '@/api/getAxios2'
import { user } from '@/store/storeName.ts'
const router = createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      redirect: "/home",
      component: main,
      children: [
        //书城
        {
          path: 'home',
          name: 'home',
          component: home,
          meta: {
            showNav: true, //显示底部导航栏
            index: 1,
            keepAlive:true
          }
        },
        //发现
        {
          path: 'about',
          name: 'about',
          component: () => import('@/views/Main/About.vue'),
          meta: {
            showNav: true,
            index: 2,
            show: true,
            keepAlive: true
          },
          children: [
            {
              path: 'CheckOk',
              component: () => import('@/views/about/CheckOk.vue'),
              meta: {
                showNav: false,
                show: false
              }
            }
          ]
        },
        //书架
        {
          path: 'BookShelf',
          name: 'BookShelf',
          component: () => import('@/views/Main/BookShelf.vue'),
          meta: {
            showNav: true,
            index: 0,
            keepAlive: true
          }
        },
        {
          path: 'Person',
          name: 'Person',
          component: () => import('@/views/Main/Person.vue'),
          meta: {
            showNav: true,
            index: 3,
            keepAlive: true
          }
        },
        //书籍详情
        {
          path: 'BookInfo',
          component: () => import('@/views/about/BookInfo.vue')
        },
        //目录
        {
          path: 'BookMenu',

          component: () => import('@/views/about/BookMenu.vue')
        },
        //排行
        {
          path: 'Ranking',
          component: () => import('@/views/about/Ranking.vue')
        },
        //话题
        {
          path: 'Taking',
          component: () => import('@/views/about/TakingView.vue'),
          meta: {
            show: true
          },
          children: [
            {
              path: 'activity',
              component: () => import('@/views/about/ActivityView.vue'),
              meta: {
                show: false
              }
            }
          ]
        },
        //作者
        {
          path: 'AuthorPage',
          component: () => import('@/views/about/Author.vue'),
          meta: {
            show: true
          },
          children: [
            {
              path: 'authorMessage',
              component: AuthorMessage,
              meta: {
                show: false
              }
            },
            {
              path: 'authorFormate',
              component: AuthorFormate,
              meta: {
                show: false
              }
            },
          ]
        },
        //最近阅读
        {
          path: 'Recently',
          component: () => import('@/views/about/RecentlyView.vue')
        },
        //收藏
        {
          path: 'Like',
          component: () => import('@/views/about/LikeView.vue')
        },
        //评论区
        {
          path: 'CommentView',
          component: () => import('@/views/about/CommentView.vue')
        },
        //收藏
        {
          path: 'Collect',
          component: () => import('@/views/about/CollectView.vue')
        },
        //搜索
        // {
        //   path: 'Search',
        //   component: () => import('@/views/about/SearchView.vue')
        // },
      ]
    },
    // 书库
    {
      path: '/bookstore',
      name: 'bookstore',
      component: () => import('@/book-store/BookStoreOne.vue'),
    },
    // 书库标签
    {
      path: '/detailtag',
      name: 'detailtag',
      component: () => import('@/book-store/DetailTag.vue'),
    },
    // 书库类型
    {
      path: '/detailtype',
      name: 'detailtype',
      component: () => import('@/book-store/DetailType.vue'),
    },

    { path: "/login", name: "login", component: () => import("@/views/Login.vue") },
    { path: "/reading", name: "reading", component: () => import("@/views/ReadingView.vue") },
    { path: "/chapterEvaluate", name: "chapterEvaluate", component: () => import('@/views/ReadingView/ChapterEvaluate.vue') },
    { path: "/setting", name: "setting", component: () => import('@/views/Person/Setting.vue') },


    //本期热销
    {
      path: '/hotview',
      name: 'hotview',
      component: () => import("@/book-city/currentHot/HotView.vue")
    },

    //新书速递
    {
      path: '/newbookview',
      name: 'newbookview',
      component: () => import("@/book-city/newBook/BookView.vue")
    },
    {
      path: '/smallview',
      name: 'smallview',
      component: () => import("@/book-city/smallCompile/smallView.vue")
    },

    // 书城-免费
    {
      path: '/freeview',
      name: 'freeview',
      component: () => import("@/book-free/FreeView.vue")
    },
    {
      path: '/newbookType',
      name: 'newbookType',
      component: () => import("@/book-newbook/NewBookView.vue")
    },

    // 搜索
    {
      path: '/search',
      name: 'search',
      component: () => import("@/Search/SearchView.vue")
    },

    { path: "/login", name: "login", component: () => import("@/views/Login.vue") },
    { path: "/reading", name: "reading", component: () => import("@/views/ReadingView.vue") },
    { path: "/chapterEvaluate", name: "chapterEvaluate", component: () => import('@/views/ReadingView/ChapterEvaluate.vue') },
    {
      path: "/login", name: "login", component: () => import("@/views/Login.vue")
    },
    { path: "/reading", name: "reading", component: () => import("@/views/ReadingView.vue") },
    { path: "/chapterEvaluate", name: "chapterEvaluate", component: () => import('@/views/ReadingView/ChapterEvaluate.vue') },
    {
      path: "/changeMsg", name: "changeMsg", component: () => import("@/views/Person/ChangeMsg.vue"),
      beforeEnter: (to, from, next) => {
        const store = user();


        console.log("aa")
        if (!store.token) {
          next("/login?to=/Person")
        } else {
          next();
        }
      }
    },

    //专题
    {
      path: '/topic',
      name: 'topic',
      component: () => import("@/book-city/TopicDetail.vue")
    },
    // 专题列表
    {
      path: '/topicList',
      name: 'topicList',
      component: () => import("@/book-city/TopicList.vue")
    },

  ]
})
router.beforeEach((to, from, next) => {
  /**
 * pinia仓库store，存储用户数据，
 * 在请求到最新数据之前使用vuex里的数据
 */
  const store = user();
  if (store.token && store.user.uname === '还未登录') {
    getPersonMsg(store.token).then(res => {
      store.changeUser(res.data)
      console.log(res.data)
    })
  }
  next();
})

export default router